package com.manong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.domain.Store;
import com.manong.exception.BusinessExp;
import com.manong.mapper.StoreMapper;
import com.manong.query.StoreQueryObject;
import com.manong.service.StoreService;
import com.manong.vo.GoodsStoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public PageInfo query(StoreQueryObject queryObject) {
        if(queryObject.getPageSize() == -1){
            List<Store> store = storeMapper.selectForList(queryObject);
            return new PageInfo(store);
        } else {
            //PageHelper进行分页操作
            //告诉PageHelper 当前页是多少 每页显示数据
            PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
            //调用分页方法
            List<Store> store = storeMapper.selectForList(queryObject);
            return new PageInfo(store);
        }
    }

    @Override
    public void delete(Integer id) {
        if(id == null){
            throw new BusinessExp("参数不能为空");
        }
        int count = storeMapper.deleteByPrimaryKey(id);
        // count 影响行数， 如果count == 0 表示删除失败
        if(count == 0){
            throw new BusinessExp("删除失败");
        }
    }

    @Override
    public void saveOrUpdate(Store store) {

        if(store == null){
            throw new BusinessExp("非法操作");
        }
        if(store.getId() == null){
            this.save(store);
        } else {
            this.update(store);
        }
    }
    @Transactional
    @Override
    public void save(Store store) {
        // 拿到role对象
        Store storeInfo = store;
        // 把role存在数据库库中
        storeMapper.insert(storeInfo);
    }

    @Override
    public void update(Store store) {
        // vo对象中获取role对象
        Store storeInfo = store;
        storeMapper.updateByPrimaryKey(storeInfo);
    }

    @Override
    public Store get(Integer id) {
        if(id == null){
            throw new BusinessExp("id不能为空");
        }
        Store store = storeMapper.selectByPrimaryKey(id);
        if(store == null){
            throw new BusinessExp("非法操作");
        }
        return store;
    }

    @Override
    public Store getStoreByManager(Integer id) {
        if(id == null){
            throw new BusinessExp("id不能为空");
        }
        Store store = storeMapper.getStoreByManager(id);
        if(store == null){
            throw new BusinessExp("非法操作");
        }
        return store;
    }

    @Override
    public int getTotalStoreCount() {
        return storeMapper.selectAll().size();
    }

    @Override
    public List<GoodsStoreVo> getGoodsInStore(Integer storeId) {
        return storeMapper.getGoodsInStore(storeId);
    }
}
